module seek_module

contains

  integer*4 function rb_seek(ptype,TargetID)
    use log
    use params_and_constants
    use particles

    implicit none
    integer*4 :: ptype,TargetID
    integer*4 :: i1,i2,itrip,HotIndex

    itrip    = 0
    i1       = 0
    i2       = Header%NumPart_Total(ptype)-1
    HotIndex = i2/2

    selectcase(ptype)

    case(0)
       do

          if(HotIndex == i1 .AND. itrip == 0) then
             itrip = 1
          else if(HotIndex == i1 .AND. itrip == 1) then
             HotIndex = HotIndex + 1
             itrip = 2
          else if(HotIndex == i1 .AND. itrip == 2) then
             rb_seek = -1
             exit
          endif

          if(TargetID > Type0(Type0(HotIndex)%Index)%ParticleIDs) then
             i1= HotIndex
             HotIndex = i1 + (i2-i1)/2
          else if(TargetID < Type0(Type0(HotIndex)%Index)%ParticleIDs) then
             i2 = HotIndex
             HotIndex = i1 + (i2-i1)/2
          else
             rb_seek = HotIndex
             exit
          endif
       enddo

    case(1)
       do
          if(HotIndex == i1 .AND. itrip == 0) then
             itrip = 1
          else if(HotIndex == i1 .AND. itrip == 1) then
             HotIndex = HotIndex + 1
             itrip = 2
          else if(HotIndex == i1 .AND. itrip == 2) then
             rb_seek = -1
             exit
          endif

          if(TargetID > Type1(Type1(HotIndex)%Index)%ParticleIDs) then
             i1= HotIndex
             HotIndex = i1 + (i2-i1)/2
          else if(TargetID < Type1(Type1(HotIndex)%Index)%ParticleIDs) then
             i2 = HotIndex
             HotIndex = i1 + (i2-i1)/2
          else
             rb_seek = HotIndex
             exit
          endif
       enddo

    case(2)
       do
          if(HotIndex == i1 .AND. itrip == 0) then
             itrip = 1
          else if(HotIndex == i1 .AND. itrip == 1) then
             HotIndex = HotIndex + 1
             itrip = 2
          else if(HotIndex == i1 .AND. itrip == 2) then
             rb_seek = -1
             exit
          endif

          if(TargetID > Type2(Type2(HotIndex)%Index)%ParticleIDs) then
             i1= HotIndex
             HotIndex = i1 + (i2-i1)/2
          else if(TargetID < Type2(Type2(HotIndex)%Index)%ParticleIDs) then
             i2 = HotIndex
             HotIndex = i1 + (i2-i1)/2
          else
             rb_seek = HotIndex
             exit
          endif
       enddo

    case(3)
       do
          if(HotIndex == i1 .AND. itrip == 0) then
             itrip = 1
          else if(HotIndex == i1 .AND. itrip == 1) then
             HotIndex = HotIndex + 1
             itrip = 2
          else if(HotIndex == i1 .AND. itrip == 2) then
             rb_seek = -1
             exit
          endif

          if(TargetID > Type3(Type3(HotIndex)%Index)%ParticleIDs) then
             i1= HotIndex
             HotIndex = i1 + (i2-i1)/2
          else if(TargetID < Type3(Type3(HotIndex)%Index)%ParticleIDs) then
             i2 = HotIndex
             HotIndex = i1 + (i2-i1)/2
          else
             rb_seek = HotIndex
             exit
          endif
       enddo

    case(4)
       
       do

          if(HotIndex == i1 .AND. itrip == 0) then
             itrip = 1
          else if(HotIndex == i1 .AND. itrip == 1) then
             HotIndex = min(HotIndex + 1,size(Type4(:)%Index)-1)
             itrip = 2
          else if(HotIndex == i1 .AND. itrip == 2) then
             rb_seek = -1
             exit
          endif

          if(TargetID > Type4(Type4(HotIndex)%Index)%ParticleIDs) then
             i1= HotIndex
             HotIndex = i1 + (i2-i1)/2
          else if(TargetID < Type4(Type4(HotIndex)%Index)%ParticleIDs) then
             i2 = HotIndex
             HotIndex = i1 + (i2-i1)/2
          else
             rb_seek = HotIndex
             exit
          endif
       enddo

    case(5)
       do
          if(HotIndex == i1 .AND. itrip == 0) then
             itrip = 1
          else if(HotIndex == i1 .AND. itrip == 1) then
             HotIndex = HotIndex + 1
             itrip = 2
          else if(HotIndex == i1 .AND. itrip == 2) then
             rb_seek = -1
             exit
          endif

          if(TargetID > Type5(Type5(HotIndex)%Index)%ParticleIDs) then
             i1= HotIndex
             HotIndex = i1 + (i2-i1)/2
          else if(TargetID < Type5(Type5(HotIndex)%Index)%ParticleIDs) then
             i2 = HotIndex
             HotIndex = i1 + (i2-i1)/2
          else
             rb_seek = HotIndex
             exit
          endif
       enddo

    endselect

    return
  end function rb_seek


end module seek_module
